package org.practice.action;

import java.io.PrintWriter;
import java.util.Arrays;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class TestAjaxAction extends ActionSupport{
	private String userName;
	@Override
	public String execute() throws Exception {
		// 设置响应的数据不缓存,ajax要求数据实时性高
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setHeader("pragma","no-cache");
		response.setHeader("cache-control","no-cache,must-revalidate");
		response.setDateHeader("Expires", 0);
		response.setContentType("text/html;charset=UTF-8");//设置ajax响应字符编码，可以req.responseText防止乱码
		setUserName(new String(userName.getBytes("ISO-8859-1"),"UTF-8"));//get请求获取参数乱码或配置tomcat server.xml
//		System.out.println("访问数据库==========="+userName+"\t"+userName.trim().length()+"\t"+aa);
		PrintWriter pw=response.getWriter();
		String[] content=new String[]{"ActionScript\n","AppleScript\n","BASIC\n","C++\n",
				"COBOL\n","Erlang\n","Erlang\n","Fortran\n","Groovy\n","Java\n","JavaScript\n",
				"Lisp\n","Python\n","Ruby\n","Scala\n","青菜\n","青菜汤\n","青菜肉丝汤\n"};
		if(userName.trim().length()>0){
			for(int i=0;i<content.length;i++){
				if(content[i].startsWith(userName)){
					pw.write(content[i]);
				}
			}
		}else{
			pw.write("ActionScript\nAppleScript\nBASIC\n");
		}
		pw.flush();
		return null;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
}
